//**********************************************************
//    File Name: FrmClientSearch.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    This form is used for searching a particular Client using first name or last name. If none are specified, all Clients will be 
//    displayed.
//	  Copyright 2007
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Library;


namespace Elderhealth
{
    public partial class FrmClientSearch : Form
    {

        private ElderHealthFormType mFollowThroughFormType;
        private bool mUseFollowForm;

        /// <summary>
        /// Default constructor
        /// </summary>
        public FrmClientSearch()
        {
            mUseFollowForm = false;
            InitializeComponent();
        }

        /// <summary>
        /// Overloaded constructor for FrmClientSearch.cs
        /// </summary>

        public FrmClientSearch(ElderHealthFormType followThroughFormType)
        {
            mFollowThroughFormType = followThroughFormType;
            mUseFollowForm = true;
            InitializeComponent();
        }

        /// <summary>
        /// Generate the list of Clients to be displayed based on the Search Criteria that user provided.
        /// </summary>
        private void Search_Click(object sender, EventArgs e)
        {
            for (int iCntr = ClientDisplaydataGrid.RowCount - 1; iCntr >= 0; iCntr = iCntr - 1)
            {
                ClientDisplaydataGrid.Rows.RemoveAt(iCntr);
            } 
                
            if (String.IsNullOrEmpty(ClientFName.Text) && String.IsNullOrEmpty(ClientLName.Text))
            {
                MessageBox.Show("Please provide a valid First Name or a Last Name");                
            }
            else
            {
                PopulateDataGridView();
            }
        }

        /// <summary>
        /// Display the list of clients.
        /// </summary>
        private void PopulateDataGridView()
        {
            ArrayList ClientList;
            ClientList = Library.Client.GetClientList(ClientFName.Text, ClientLName.Text);
            Client Clientdetail;
            if (ClientList.Count == 0)
            {
                NoRecord.Visible = true;
            }
            else
            {
                for (int i = 0; i < ClientList.Count; i++)
                {
                    Clientdetail = (Client)ClientList[i];
                    String Name = Clientdetail.GetFirstName() + " " + Clientdetail.GetLastName();
                    String Add = Clientdetail.GetCity() + "," + Clientdetail.GetState();
                    ClientDisplaydataGrid.Rows.Add(0, Name, Add, Clientdetail.GetDateOfBirth().ToShortDateString(), Clientdetail.GetUserID().ToString());
                }
                NoRecord.Visible = false;
            }
            
        }

        /// <summary>
        /// User decides to cancel the task in hand and hence wishes to close the form.
        /// </summary>

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Continue_Click(object sender, EventArgs e)
        {
            FrmCreateClientSchedule frm = new FrmCreateClientSchedule();         
            frm.Show();
        }

        /// <summary>
        /// Display the details for the Client selected.
        /// </summary>

        private void ClientDisplaydataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            long TmpClientID;
            int TmpSelRow = ClientDisplaydataGrid.SelectedCells[0].RowIndex;
            String SelectedRowOutput = ClientDisplaydataGrid.Rows[TmpSelRow].Cells[4].Value.ToString();
            TmpClientID = long.Parse(SelectedRowOutput);
            this.Close();

            if (mUseFollowForm)
            {
                // for each follow through form type, launch the appropriate forms
                if (ElderHealthFormType.ViewClientForm == mFollowThroughFormType)
                {
                    FrmViewClient frm = new FrmViewClient(TmpClientID);
                    frm.Show();
                }
                else if (ElderHealthFormType.CreateClientScheduleForm == mFollowThroughFormType)
                {
                    FrmCreateClientSchedule frm = new FrmCreateClientSchedule(TmpClientID);
                    frm.Show();
                }
            }
        }

        /// <summary>
        /// Clear the form.
        /// </summary>
        private void Clear_Click(object sender, EventArgs e)
        {
            for (int iCntr = ClientDisplaydataGrid.RowCount - 1; iCntr >= 0; iCntr = iCntr - 1)
            {
                ClientDisplaydataGrid.Rows.RemoveAt(iCntr);
            }
            NoRecord.Visible = false;
            ClientFName.Text = "";
            ClientLName.Text = "";
        }
    }
}